package org.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.erp.domain.ProFeedback;
import org.example.erp.domain.ProTask;
import org.example.erp.domain.ProWorkorder;
import org.example.erp.domain.ProcedureDetails;
import org.example.erp.mapper.ProFeedbackMapper;
import org.example.erp.service.ProFeedbackService;
import org.example.erp.service.ProTaskService;
import org.example.erp.service.ProWorkorderService;
import org.example.erp.service.ProceduresDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class ProFeedbackServiceImpl extends ServiceImpl<ProFeedbackMapper, ProFeedback> implements ProFeedbackService {
    @Autowired
    ProTaskService proTaskService;
    @Autowired
    ProceduresDetailsService proceduresDetailsService;
    @Autowired
    ProWorkorderService proWorkorderService;

    public ProFeedback find(String workorderCode) {
        return this.getBaseMapper().find(workorderCode);
    }

    @Transactional
    public boolean updateDetail(ProFeedback proFeedback) {
        boolean a=false;
        boolean b = updateById(proFeedback);
        if (b){
            a=true;
        }

        QueryWrapper<ProTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProTask::getTaskCode, proFeedback.getTaskCode());
        ProTask one = proTaskService.getOne(queryWrapper);
        one.setQuantityProduced(proFeedback.getQuantityQualified() + one.getQuantityProduced());
        one.setQuantityQuanlify(proFeedback.getQuantityQualified() + one.getQuantityQuanlify());
        one.setQuantityUnquanlify(proFeedback.getQuantityUnquanlified() + one.getQuantityUnquanlify());
        if (one.getQuantity() - one.getQuantityQuanlify() >= 0) {
            boolean b1 = proTaskService.updateById(one);
            if (b1){
                a=true;
            }
        }else {
            a=false;
        }

        //查询出该工单产品的每个工序都完成了多少数量，并且每个工序都有数量，取最低数量修改工单表上的生产数量
        QueryWrapper<ProTask> q1 = new QueryWrapper<>();
        q1.lambda().eq(ProTask::getWorkorderCode,proFeedback.getWorkorderCode());
        List<ProTask> list = proTaskService.list(q1);
        //循环比较大小，当里面的生产数量最小为0，就说明该工单还没走完全部工序
        int i = someMethod(list);
        if (i>0){
            QueryWrapper<ProWorkorder> q2= new QueryWrapper<>();
            q2.lambda().eq(ProWorkorder::getWorkorderCode,proFeedback.getWorkorderCode());
            ProWorkorder one1 = proWorkorderService.getOne(q2);
            one1.setQuantityProduced(i);
            boolean b2 = proWorkorderService.updateById(one1);
            if (b2){
                a=true;
            }
        }
        return a;
    }
    // 判断最小方法
    public int someMethod(List<ProTask> proTasks) {
        Optional<Integer> minQuantityProduced = proTasks.stream()
                .map(ProTask::getQuantityProduced)
                .min(Integer::compareTo);

        if (minQuantityProduced.isPresent()) {
            int minValue = minQuantityProduced.get();
            System.out.println("quantityProduced 字段的最小值是: " + minValue);
            return minValue;
        } else {
            System.out.println("集合为空，没有最小值。");
            return 0;
        }
    }
}
